package com.example.controller;

import com.example.result.Result;
import com.example.context.BaseContext;
import com.example.entity.Address;
import com.example.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.math3.analysis.function.Add;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/address")
@Tag(name = "地址模块", description = "地址模块的描述")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 在地址表里面新增地址
     * @param address
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "新增地址",description="新增地址")
    public Result addaddress(@RequestBody Address address){
        addressService.addaddress(address);
        return Result.success();
    }

    /**
     * 设置地址为默认状态
     * @param address
     * @return
     */
    @PostMapping("/setdefault")
    @Operation(summary = "设置地址为默认",description="设置该地址为默认状态")
    public Result setdefault(@RequestBody Address address){
        addressService.setdefault(address);
        return Result.success();
    }
  //  localhost:8080/address/getAll/1
    /**
     * 根据用户id查询所有地址
     * @param userId
     * @return
     */
    @GetMapping("/getAll/{userId}")
    @Operation(summary = "查询当前用户所有地址",description="根据当前用户的id查询所有地址信息")
    public Result<List<Address>> getAll(@PathVariable Integer userId){
        List<Address> addressList=new ArrayList<>();
        addressList=addressService.getAll(userId);
        return Result.success(addressList);
    }

    /**
     * 查询当前用户默认地址
     * @param userId
     * @return
     */
    @GetMapping("/getDefault/{userId}")
    @Operation(summary = "查询当前用户默认地址",description="查询当前用户默认地址")
    public Result<Address> getDefault(@PathVariable Integer userId){
        return Result.success(addressService.getDefault(userId));
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    @Operation(summary = "根据id查询地址",description="根据id查询地址")
    public Result<Address> getById(@PathVariable Integer id){
        Address address=addressService.getById(id);
        return Result.success(address);
    }

    /**
     * 根据id修改地址
     * @param address
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "根据id修改地址",description="修改用户地址")
    public Result update(@RequestBody Address address){
        addressService.update(address);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "根据id删除地址",description="根据id删除地址")
    public Result deleteById(@PathVariable Integer id){
        addressService.deleteById(id);
        return Result.success();
    }
}
